function screenMoveEnd(map) {
    var bound = map.getBounds();
    var southWest = bound.getSouthWest();
    var northEast = bound.getNorthEast();

    GetHousesServlet.getHouses(southWest.lat(), southWest.lng(), northEast.lat(), northEast.lng(), function(list) {
        setToMap(map, list);
        updateHouseList(map, list);
    });

    GetHousesServlet.getOffices(southWest.lat(), southWest.lng(), northEast.lat(), northEast.lng(), function(list) {
        setToMapOffices(map, list);
        updateOfficeList(map, list);
    });

    /*var request = makeRequest();

    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status != 200) {
                alert("Search was not completed due to error: " + request.statusText);
            } else {
                var points = parsePointsText(request.responseText);
                setToMap(map, points);
                updatePointList(map, points)
            }
        }
    };

    request.open("GET", "/getHouses?" +
                        "swlat=" + southWest.lat() +
                        "&swlng=" + southWest.lng() +
                        "&nelat=" + northEast.lat() +
                        "&nelng=" + northEast.lng()
            , true);
    request.send(null);*/
}

function setToMap(map, points) {
    for (var i in points) {
        var flat = points[i];
        var latlng = new GLatLng(flat.lat, flat.lng);

        if (houseMap[flat.id] == undefined) {
            var marker = createHouse(map, latlng, flat);
            map.addOverlay(marker);
            houseMap[flat.id] = flat;
        } else {
            var oldFlat = houseMap[flat.id];
            flat.marker = oldFlat.marker;
            houseMap[flat.id] = flat;
            updateMarkerHouse(map, latlng, flat);
        }
    }
}

function setToMapOffices(map, points) {
    for (var i in points) {
        var office = points[i];
        var latlng = new GLatLng(office.lat, office.lng);

        if (officeMap[office.id] == undefined) {
            var marker = createOffice(map, latlng, office);
            map.addOverlay(marker);
            officeMap[office.id] = office;
        } else {
            var oldFlat = officeMap[office.id];
            office.marker = oldFlat.marker;
            officeMap[office.id] = office;
            updateMarkerOffice(map, latlng, office);
        }
    }
}

function updateHouseList(map, points) {
    var text = "<table width='100%' cellspacing='0'>";
    for (var i in points) {
        var flat = points[i];
        text += '<tr><td  valign="top" onclick="centerFlat(houseMap[' + flat.id + '])" style="font-family:Arial;font-size:10px"';
        text += 'bgcolor="' + (i % 2 == 0 ? '#F0F0F0' : '#E0E0E0') + '" ';
        text += ' onmousemove="this.bgColor=\'#D0D0D0\'" ';
        text += ' onmouseout="this.bgColor= ' + (i % 2 == 0 ? "'#F0F0F0'" : "'#E0E0E0'") + '" ';
        text += ' id="flatList' + flat.id + '"';
        text += ' >';

        if(flat.type == 'RENT') {
            text += '<img src="/img/cubisthome.png" align="left"> аренда '
        }
        if(flat.type == 'SALE') {
            text += '<img src="/img/homesell32.png" align="left"> продажа '
        }
        text += '<b>Квартира</b> ';
        text += flat.roomNumber + 'к ' + flat.price + flat.currency + ' / ';
        text += flat.address;
        text += '</td>';

        text += '<td bgcolor="' + (i % 2 == 0 ? '#F0F0F0' : '#E0E0E0') + '" >';
        if(readCookie('f' + flat.id) == undefined) {
            text += '<a href="javascript:void(0)" onclick="swichFavorite(\'f' + flat.id + '\',this)"><img src="/img/notfavorite.png" border="0"></a>';
        } else {
            text += '<a href="javascript:void(0)" onclick="swichFavorite(\'f' + flat.id + '\',this)"><img src="/img/favorite.png" border="0"></a>';
        }
        text += '</td></tr>';
    }
    text += "</table>";
    document.getElementById("pointList").innerHTML = text;
}

function updateOfficeList(map, points) {
    var text = "<table width='100%' cellspacing='0'>";
    for (var i in points) {
        var office = points[i];
        text += '<tr><td  valign="top" onclick="centerOffice(officeMap[' + office.id + '])" style="font-family:Arial;font-size:10px"';
        text += 'bgcolor="' + (i % 2 == 0 ? '#F0F0F0' : '#E0E0E0') + '" ';
        text += ' onmousemove="this.bgColor=\'#D0D0D0\'" ';
        text += ' onmouseout="this.bgColor= ' + (i % 2 == 0 ? "'#F0F0F0'" : "'#E0E0E0'") + '" ';
        text += ' id="flatList' + office.id + '"';
        text += ' >';

        if(office.type == 'RENT') {
            text += '<img src="/img/comerc32.png" align="left"> аренда '
        }
        if(office.type == 'SALE') {
            text += '<img src="/img/comerc32.png" align="left"> продажа '
        }
        text += '<b>Офис</b> ';
        text += office.roomNumber + 'к ' + office.price + office.currency + ' / ';
        text += office.address;
        text += '</td>';

        text += '<td bgcolor="' + (i % 2 == 0 ? '#F0F0F0' : '#E0E0E0') + '" >';
        if(readCookie('o' + office.id) == undefined) {
            text += '<a href="javascript:void(0)" onclick="swichFavorite(\'o' + office.id + '\',this)"><img src="/img/notfavorite.png" border="0"></a>';
        } else {
            text += '<a href="javascript:void(0)" onclick="swichFavorite(\'o' + office.id + '\',this)"><img src="/img/favorite.png" border="0"></a>';
        }
        text += '</td></tr>';
    }
    text += "</table>";
    document.getElementById("pointListOffices").innerHTML = text;
}

function saveForm(formId, action) {
    var text = '';
    var elements = document.getElementById(formId).elements;
    for (var i in elements) {
        var element = elements[i];
        if (element != undefined && element.name != undefined && element.name != null && element.name != ''
                && element.value != undefined && element.value != '') {
            text += element.name + '=' + element.value.replaceAll('\n', '%0A') + '&';
        }
    }
    setResponseToElement(action + '?' + text, 'properties');
}
